package com.seamtop.cuber.icm;

import com.seamtop.cuber.common.entriy.TransData;
import com.seamtop.cuber.common.metadata.Column;
import com.seamtop.cuber.common.metadata.TableMetaData;
import com.seamtop.cuber.common.util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by feng on 2015/8/17.
 */
public enum IcmTaskCombine {

    INSTANCE;

    public static List<TransData> getJoinTaskList(String familyName,HashMap<String,String> columnMap,TableMetaData tableMetaData){
        List<TransData> transDataList = new ArrayList<TransData>();
        for(String s:columnMap.keySet()){
            Column column = tableMetaData.getColumnMap().get(s);
            TransData transData = new TransData();
            transData.setFamilyName(familyName);
            String match = tableMetaData.getColumnMap().get(s).getMatch();
            transData.setColumnName(match);
            //transData.setColumnName(s);
            transData.setValue(columnMap.get(s));
            transDataList.add(transData);
            transData.setValueType(column.getColumnType());
        }
        return transDataList;
    }


    public static List<TransData> getUpdateTaskList(HashMap<String,Object> dataMap,TableMetaData tableMetaData){
        HashMap<String,Column> columnMap = tableMetaData.getColumnMap();
        List<TransData> list = new ArrayList<TransData>();
        String keyName = tableMetaData.getRowKey().getKeyName();
//        String rowKey = dataMap.get(keyName);
        for(String params : dataMap.keySet()){
            if(params.equals(keyName)){
                continue;
            }
            Column column = columnMap.get(params);
            String columnName = column.getColumnName();
            String value = (String)dataMap.get(columnName);
            TransData transData = new TransData();
            transData.setFamilyName(column.getFamilyName());
            transData.setColumnName(column.getMatch());
            //transData.setColumnName(columnName);
            transData.setValue(value);
            transData.setValueType(column.getColumnType());
            list.add(transData);
        }
        return list;
    }

    public static List<TransData> getIncrementTaskList(HashMap<String,Object> dataMap,TableMetaData tableMetaData){
        HashMap<String,Column> columnMap = tableMetaData.getColumnMap();
        List<TransData> list = new ArrayList<TransData>();
        String keyName = null;
        if(!tableMetaData.isVirtualTable()){
            keyName = tableMetaData.getRowKey().getKeyName();
        }
        for (String params : dataMap.keySet()){
            if(!StringUtil.isEmpty(keyName) && params.equals(keyName)){
                continue;
            }
            Column column = columnMap.get(params);
            if(!column.isIncrement()){
                continue;
            }
            String columnName = column.getColumnName();
            String value = (String)dataMap.get(columnName);
            TransData transData = new TransData();
            transData.setFamilyName(column.getFamilyName());
            transData.setColumnName(column.getMatch());
            transData.setValue(value);
            transData.setValueType(column.getColumnType());
            list.add(transData);
        }
        return list;
    }


    public static List<TransData> getAddTaskList(HashMap<String,Object> dataMap,TableMetaData tableMetaData){
        HashMap<String,Column> columnMap = tableMetaData.getColumnMap();
        List<TransData> list = new ArrayList<TransData>();
        String keyName = null;
        if(!tableMetaData.isVirtualTable()){
            keyName = tableMetaData.getRowKey().getKeyName();
        }

        for(String params : dataMap.keySet()){
            if(!StringUtil.isEmpty(keyName) && params.equals(keyName)){
                continue;
            }
            Column column = columnMap.get(params);
            if(column.isIncrement()){
                continue;
            }
            String columnName = column.getColumnName();
            String value = (String)dataMap.get(columnName);
            TransData transData = new TransData();
            transData.setFamilyName(column.getFamilyName());
            transData.setColumnName(column.getMatch());
            //transData.setColumnName(columnName);
            transData.setValue(value);
            transData.setValueType(column.getColumnType());
            list.add(transData);
        }
        return list;
    }
}
